// pages/setManager/setManager.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req:false,
    show: null,
    supervisorshow:false,
    pageEnd: false,
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    list: [],
    supervisor:{},
  },
  check: function() {
    this.toggle("showApp", true);
  },
  onClose: function() {
    this.toggle("showApp", false);
  },
  contact: function(e) {
    console.log(e);
  },
  supervisorshow(){
    this.toggle('supervisorshow',true)
  },
  show(e) {
    var dataset = e.currentTarget.dataset;
    this.toggle("show", dataset.index)
  },
  hide() {
    this.toggle("show", null)
    this.toggle('supervisorshow', false)
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toAdd() {
    wx.navigateTo({
      url: '/pages/addManagers/addManagers?id=' + this.data.orgId
    })
  },
  toDel(e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    wx.showModal({
      content: '您确定要将' + dataset.name+'移除管理员吗？',
      cancelColor:'#333333',
      confirmColor:'#333333',
      success(res) {
        if (res.confirm) {
          app.req({
            url: 'orgmini//manager/admin/remove',
            data: {
              id: dataset.id
            },
            success: function (res) {
              if (200 == res.code) {
                var list = that.data.list.filter(function (item) {
                  return item.id != dataset.id
                });
                that.toggle('list', list)
                wx.showToast({
                  title: '移除成功',
                  duration: 2000
                })
              }
            }
          });
        }
      }
    })
  },
  list: function(append) {
    var that = this;
    that.toggle("req", false);
    var param = that.data.param
    app.req({
      // url: 'orgmini/manager/admin/list',
      url:'orgmini/org/newOrgManagerList',
      data: param,
      success: function(res) {
        if (200 == res.code) {
         
          var list = res.data.orgManagerEntityPage.rows;
          if (!list || list.length < param.pageSize) {
            that.toggle("pageEnd", true);
          }
          that.toggle('supervisor', res.data.supervisor)
          that.toggle("list", append ? that.data.list.concat(list) : list);
          that.toggle("req", true);
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.toggle("param.orgId", options.id);
    that.toggle("orgId", options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    app.getUserInfo(function(userInfo) {
      that.toggle("uid", userInfo.id);
      that.toggle("param.pageNumber", 1);
      that.toggle("pageEnd", false);
      that.list()
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.toggle("param.pageNumber", 1);
      that.toggle("pageEnd", false);
      that.list();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (!that.data.pageEnd) {
      that.toggle("param.pageNumber", that.data.param.pageNumber + 1);
      that.list(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})